package com.yuanqunsoft.alimigration.action;

import java.util.List;

import org.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.yuanqunsoft.alimigration.bean.RuleBean;
import com.yuanqunsoft.alimigration.model.Remediation;
import com.yuanqunsoft.alimigration.model.Rule;
import com.yuanqunsoft.alimigration.service.IRemediationService;
import com.yuanqunsoft.alimigration.service.IRuleService;
import com.yuanqunsoft.alimigration.utils.JsonResult;

public class RuleAction extends ActionSupport {

    private List<?> selectedRemediations;
    private IRemediationService remediationService;
    private JsonResult result;
    private boolean success;
    private IRuleService ruleService;
    private String rulName;
    private int rulId;
    private List<?> datas;
    private int remId;

    private int start;
    private int limit;

    public String Page() {
        return "page";
    }

    public String conRemediation() {
        RuleBean ruleBean = this.ruleService.getRuleById(rulId);
        this.ruleService.selectRemediation(ruleBean, remId);
        this.setSuccess(true);
        return "success";
    }

    public String delRule() {
        try {
            RuleBean ruleBean = this.getRuleService().getRuleById(rulId);
            if (this.getRuleService().deleteRule(ruleBean)) {
                this.setSuccess(true);
            } else {
                this.setSuccess(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.setSuccess(false);
        }
        return "success";
    }

    public String ruleList() {

        List<RuleBean> list = this.getRuleService().getAllRule(start, limit);
        setResult(new JsonResult(list, this.ruleService.getAllPage()));
        this.setSuccess(true);
        return "success";
    }

    public String addRule() {
        RuleBean ruleBean;
        ruleBean = this.getRuleBean();
        if (this.ruleService.isRuleNameExisted(ruleBean)) {
            this.setSuccess(false);
            return "success";
        }
        if (this.ruleService.addRule(ruleBean, 1)) {
            this.setSuccess(true);
        } else {
            this.setSuccess(true);
        }
        return "success";

    }

    public String modifyRule() {
        RuleBean ruleBean;
        ruleBean = this.getRuleBean();
        ruleBean.setRulId(rulId);
        try {
            this.ruleService.update(ruleBean);
            this.setSuccess(true);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            this.setSuccess(false);
            return "success";
        }
    }

    public String getRemediations() {
        List<Remediation> list = this.remediationService.getAllRemediations();
        setResult(new JsonResult(list, list.size()));
        this.setSuccess(true);
        return "success";

    }

    public String getSelectedRemediation() {
        List<Remediation> list = this.remediationService.getAllRemediations();
        setResult(new JsonResult(list, list.size()));
        this.setSuccess(true);
        return "success";

    }

    public RuleBean getRuleBean() {
        JSONObject json = new JSONObject(datas.get(0).toString());// 转换为json对象格式
        // 将json对象转换为javaBean
        RuleBean ruleBean = new RuleBean();
        String rulName = json.getString("rulName");
        String rulDescription = json.getString("rulDescription");
        String rulSeverity = json.getString("rulSeverity");
        String rulCategory = json.getString("rulCategory");
        String rulEnabled = json.getString("rulEnabled");
        String rulFirstAlerts = json.getString("rulFirstAlerts");
        String rulFirstDaysEach = json.getString("rulFirstDaysEach");
        String rulNextAlerts = json.getString("rulNextAlerts");
        String rulNextDaysEach = json.getString("rulNextDaysEach");
        String rulAllFurtherDayEach = json.getString("rulAllFurtherDayEach");
        if (!rulFirstAlerts.equals("")) {
            ruleBean.setRulFirstAlerts(Integer.valueOf(rulFirstAlerts));
        }
        if (!rulAllFurtherDayEach.equals("")) {
            ruleBean.setRulAllFurtherDayEach(Float.valueOf(rulAllFurtherDayEach));
        }
        if (!rulNextAlerts.equals("")) {
            ruleBean.setRulNextAlerts(Integer.valueOf(rulNextAlerts));
        }
        if (!rulFirstDaysEach.equals("")) {
            ruleBean.setRulFirstDaysEach(Float.valueOf(rulFirstDaysEach));
        }
        if (!rulNextDaysEach.equals("")) {
            ruleBean.setRulNextDaysEach(Float.valueOf(rulNextDaysEach));
        }
        ruleBean.setRulCategory(Short.valueOf(rulCategory));
        ruleBean.setRulEnabled(Short.valueOf(rulEnabled));
        ruleBean.setRulDescription(rulDescription);
        ruleBean.setRulName(rulName);
        ruleBean.setRulSeverity(Short.valueOf(rulSeverity));
        return ruleBean;
    }

    public String updateRuleSet() {

        RuleBean ruleBean = this.getRuleBean();
        this.ruleService.update(ruleBean);
        this.setSuccess(true);
        return "success";
    }

    public JsonResult getResult() {
        return result;
    }

    public void setResult(JsonResult result) {
        this.result = result;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public IRuleService getRuleService() {
        return ruleService;
    }

    public void setRuleService(IRuleService ruleService) {
        this.ruleService = ruleService;
    }

    public String getRulName() {
        return rulName;
    }

    public void setRulName(String rulName) {
        this.rulName = rulName;
    }

    public int getRulId() {
        return rulId;
    }

    public void setRulId(int rulId) {
        this.rulId = rulId;
    }

    public List<?> getDatas() {
        return datas;
    }

    public void setDatas(List<?> datas) {
        this.datas = datas;
    }

    public IRemediationService getRemediationService() {
        return remediationService;
    }

    public void setRemediationService(IRemediationService remediationService) {
        this.remediationService = remediationService;
    }

    public int getRemId() {
        return remId;
    }

    public void setRemId(int remId) {
        this.remId = remId;
    }

    public List<?> getSelectedRemediations() {
        return selectedRemediations;
    }

    public void setSelectedRemediations(List<?> selectedRemediations) {
        this.selectedRemediations = selectedRemediations;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

}
